{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "notes"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from rich import print\n",
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\"\n",
    "%load_ext rich"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "<div class=\"jumbotron\">\n",
    "    <h1 class=\"display-1\">线性代数</h1>\n",
    "    <hr class=\"my-4\">\n",
    "    <p>主讲：李岩</p>\n",
    "    <p>管理学院</p>\n",
    "    <p>liyan@cumtb.edu.cn</p>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 向量与矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- 列向量是向量的默认方向"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "$$\\mathbf{x} =\\begin{bmatrix}x_{1}  \\\\x_{2}  \\\\ \\vdots  \\\\x_{n}\\end{bmatrix},$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.473385Z",
     "iopub.status.busy": "2022-07-31T02:49:25.473178Z",
     "iopub.status.idle": "2022-07-31T02:49:25.478396Z",
     "shell.execute_reply": "2022-07-31T02:49:25.477767Z"
    },
    "origin_pos": 6,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m, \u001b[1;36m1\u001b[0m, \u001b[1;36m2\u001b[0m, \u001b[1;36m3\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(4)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### 张量的长度、维度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "\\begin{definition}\\label{def:dimension}\n",
    "向量的维度（dimension）：向量的长度（包含的元素个数）\n",
    "\\end{definition}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- 访问张量长度的方法\n",
    "    - `Python`内置的`len()`函数\n",
    "    - 张量的`shape`属性，一个元素组，列出了张量沿每个轴的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.488926Z",
     "iopub.status.busy": "2022-07-31T02:49:25.488725Z",
     "iopub.status.idle": "2022-07-31T02:49:25.493225Z",
     "shell.execute_reply": "2022-07-31T02:49:25.492640Z"
    },
    "origin_pos": 14,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">张量x的维度为4\n",
       "</pre>\n"
      ],
      "text/plain": [
       "张量x的维度为4\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'张量x的维度为{len(x)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.496049Z",
     "iopub.status.busy": "2022-07-31T02:49:25.495847Z",
     "iopub.status.idle": "2022-07-31T02:49:25.500197Z",
     "shell.execute_reply": "2022-07-31T02:49:25.499609Z"
    },
    "origin_pos": 18,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">张量x的形状为torch.Size</span><span style=\"font-weight: bold\">([</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span><span style=\"font-weight: bold\">])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1;35m张量x的形状为torch.Size\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'张量x的形状为{x.shape}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### 矩阵的转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.503025Z",
     "iopub.status.busy": "2022-07-31T02:49:25.502706Z",
     "iopub.status.idle": "2022-07-31T02:49:25.507724Z",
     "shell.execute_reply": "2022-07-31T02:49:25.507121Z"
    },
    "origin_pos": 22,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m,  \u001b[1;36m1\u001b[0m,  \u001b[1;36m2\u001b[0m,  \u001b[1;36m3\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m4\u001b[0m,  \u001b[1;36m5\u001b[0m,  \u001b[1;36m6\u001b[0m,  \u001b[1;36m7\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m,  \u001b[1;36m9\u001b[0m, \u001b[1;36m10\u001b[0m, \u001b[1;36m11\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m12\u001b[0m, \u001b[1;36m13\u001b[0m, \u001b[1;36m14\u001b[0m, \u001b[1;36m15\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m16\u001b[0m, \u001b[1;36m17\u001b[0m, \u001b[1;36m18\u001b[0m, \u001b[1;36m19\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = torch.arange(20).reshape(5, 4)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.511328Z",
     "iopub.status.busy": "2022-07-31T02:49:25.510560Z",
     "iopub.status.idle": "2022-07-31T02:49:25.517098Z",
     "shell.execute_reply": "2022-07-31T02:49:25.516299Z"
    },
    "origin_pos": 26,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">张量A的转置为\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">8</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">12</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">16</span><span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">13</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">17</span><span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">14</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">18</span><span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">7</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">11</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">15</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">19</span><span style=\"font-weight: bold\">]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "张量A的转置为\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m,  \u001b[1;36m4\u001b[0m,  \u001b[1;36m8\u001b[0m, \u001b[1;36m12\u001b[0m, \u001b[1;36m16\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m1\u001b[0m,  \u001b[1;36m5\u001b[0m,  \u001b[1;36m9\u001b[0m, \u001b[1;36m13\u001b[0m, \u001b[1;36m17\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m2\u001b[0m,  \u001b[1;36m6\u001b[0m, \u001b[1;36m10\u001b[0m, \u001b[1;36m14\u001b[0m, \u001b[1;36m18\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m3\u001b[0m,  \u001b[1;36m7\u001b[0m, \u001b[1;36m11\u001b[0m, \u001b[1;36m15\u001b[0m, \u001b[1;36m19\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'张量A的转置为\\n{A.T}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- 对称矩阵（symmetric matrix）$\\mathbf{A}$等于其转置：$\\mathbf{A} = \\mathbf{A}^\\top$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.521175Z",
     "iopub.status.busy": "2022-07-31T02:49:25.520512Z",
     "iopub.status.idle": "2022-07-31T02:49:25.527941Z",
     "shell.execute_reply": "2022-07-31T02:49:25.527038Z"
    },
    "origin_pos": 30,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">生成一个对称张量B\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span><span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span><span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span><span style=\"font-weight: bold\">]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "生成一个对称张量B\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m1\u001b[0m, \u001b[1;36m2\u001b[0m, \u001b[1;36m3\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m2\u001b[0m, \u001b[1;36m0\u001b[0m, \u001b[1;36m4\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m3\u001b[0m, \u001b[1;36m4\u001b[0m, \u001b[1;36m5\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "B = torch.tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]])\n",
    "print(f'生成一个对称张量B\\n{B}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.531482Z",
     "iopub.status.busy": "2022-07-31T02:49:25.530835Z",
     "iopub.status.idle": "2022-07-31T02:49:25.537377Z",
     "shell.execute_reply": "2022-07-31T02:49:25.536523Z"
    },
    "origin_pos": 34,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m\u001b[3;92mTrue\u001b[0m, \u001b[3;92mTrue\u001b[0m, \u001b[3;92mTrue\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[3;92mTrue\u001b[0m, \u001b[3;92mTrue\u001b[0m, \u001b[3;92mTrue\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[3;92mTrue\u001b[0m, \u001b[3;92mTrue\u001b[0m, \u001b[3;92mTrue\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B == B.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 张量的运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "### 张量运算的基本性质"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- 给定具有相同形状的任意两个张量，任何按元素二元运算的结果都将是相同形状的张量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.551992Z",
     "iopub.status.busy": "2022-07-31T02:49:25.551379Z",
     "iopub.status.idle": "2022-07-31T02:49:25.560709Z",
     "shell.execute_reply": "2022-07-31T02:49:25.559920Z"
    },
    "origin_pos": 42,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m.,  \u001b[1;36m1\u001b[0m.,  \u001b[1;36m2\u001b[0m.,  \u001b[1;36m3\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m4\u001b[0m.,  \u001b[1;36m5\u001b[0m.,  \u001b[1;36m6\u001b[0m.,  \u001b[1;36m7\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m.,  \u001b[1;36m9\u001b[0m., \u001b[1;36m10\u001b[0m., \u001b[1;36m11\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m12\u001b[0m., \u001b[1;36m13\u001b[0m., \u001b[1;36m14\u001b[0m., \u001b[1;36m15\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m16\u001b[0m., \u001b[1;36m17\u001b[0m., \u001b[1;36m18\u001b[0m., \u001b[1;36m19\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = torch.arange(20, dtype=torch.float32).reshape(5, 4)\n",
    "B = A.clone() # 通过分配新内存，将A的一个副本分配给B\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">A+B为\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">8</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">12</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">14</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">16</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">18</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">20</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">22</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">24</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">26</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">28</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">30</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">32</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">34</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">36</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">38</span>.<span style=\"font-weight: bold\">]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "A+B为\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m.,  \u001b[1;36m2\u001b[0m.,  \u001b[1;36m4\u001b[0m.,  \u001b[1;36m6\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m., \u001b[1;36m10\u001b[0m., \u001b[1;36m12\u001b[0m., \u001b[1;36m14\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m16\u001b[0m., \u001b[1;36m18\u001b[0m., \u001b[1;36m20\u001b[0m., \u001b[1;36m22\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m24\u001b[0m., \u001b[1;36m26\u001b[0m., \u001b[1;36m28\u001b[0m., \u001b[1;36m30\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m32\u001b[0m., \u001b[1;36m34\u001b[0m., \u001b[1;36m36\u001b[0m., \u001b[1;36m38\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'A+B为\\n{A + B}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### 矩阵按元素乘法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- 两个矩阵的按元素乘法称为*Hadamard积*（Hadamard product）（数学符号$\\odot$）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "$$\n",
    "\\mathbf{A} \\odot \\mathbf{B} =\n",
    "\\begin{bmatrix}\n",
    "    a_{11}  b_{11} & a_{12}  b_{12} & \\dots  & a_{1n}  b_{1n} \\\\\n",
    "    a_{21}  b_{21} & a_{22}  b_{22} & \\dots  & a_{2n}  b_{2n} \\\\\n",
    "    \\vdots & \\vdots & \\ddots & \\vdots \\\\\n",
    "    a_{m1}  b_{m1} & a_{m2}  b_{m2} & \\dots  & a_{mn}  b_{mn}\n",
    "\\end{bmatrix}.\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.564286Z",
     "iopub.status.busy": "2022-07-31T02:49:25.563751Z",
     "iopub.status.idle": "2022-07-31T02:49:25.568979Z",
     "shell.execute_reply": "2022-07-31T02:49:25.568278Z"
    },
    "origin_pos": 46,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">张量A和B的Hadamard积为\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[</span>  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>.,   <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>.,   <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>.,   <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">16</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">25</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">36</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">49</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">64</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">81</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">100</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">121</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">144</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">169</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">196</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">225</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">256</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">289</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">324</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">361</span>.<span style=\"font-weight: bold\">]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "张量A和B的Hadamard积为\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m  \u001b[1;36m0\u001b[0m.,   \u001b[1;36m1\u001b[0m.,   \u001b[1;36m4\u001b[0m.,   \u001b[1;36m9\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m16\u001b[0m.,  \u001b[1;36m25\u001b[0m.,  \u001b[1;36m36\u001b[0m.,  \u001b[1;36m49\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m64\u001b[0m.,  \u001b[1;36m81\u001b[0m., \u001b[1;36m100\u001b[0m., \u001b[1;36m121\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m144\u001b[0m., \u001b[1;36m169\u001b[0m., \u001b[1;36m196\u001b[0m., \u001b[1;36m225\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m256\u001b[0m., \u001b[1;36m289\u001b[0m., \u001b[1;36m324\u001b[0m., \u001b[1;36m361\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'张量A和B的Hadamard积为\\n{A * B}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.572916Z",
     "iopub.status.busy": "2022-07-31T02:49:25.572220Z",
     "iopub.status.idle": "2022-07-31T02:49:25.579768Z",
     "shell.execute_reply": "2022-07-31T02:49:25.578881Z"
    },
    "origin_pos": 50,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">X是\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">7</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">8</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">11</span><span style=\"font-weight: bold\">]]</span>,\n",
       "\n",
       "        <span style=\"font-weight: bold\">[[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">12</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">13</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">14</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">15</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">16</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">17</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">18</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">19</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">20</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">21</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">22</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">23</span><span style=\"font-weight: bold\">]]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "X是\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m,  \u001b[1;36m1\u001b[0m,  \u001b[1;36m2\u001b[0m,  \u001b[1;36m3\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m \u001b[1;36m4\u001b[0m,  \u001b[1;36m5\u001b[0m,  \u001b[1;36m6\u001b[0m,  \u001b[1;36m7\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m,  \u001b[1;36m9\u001b[0m, \u001b[1;36m10\u001b[0m, \u001b[1;36m11\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m,\n",
       "\n",
       "        \u001b[1m[\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m12\u001b[0m, \u001b[1;36m13\u001b[0m, \u001b[1;36m14\u001b[0m, \u001b[1;36m15\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m\u001b[1;36m16\u001b[0m, \u001b[1;36m17\u001b[0m, \u001b[1;36m18\u001b[0m, \u001b[1;36m19\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m\u001b[1;36m20\u001b[0m, \u001b[1;36m21\u001b[0m, \u001b[1;36m22\u001b[0m, \u001b[1;36m23\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">a+X是\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">7</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">8</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">11</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">12</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">13</span><span style=\"font-weight: bold\">]]</span>,\n",
       "\n",
       "        <span style=\"font-weight: bold\">[[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">14</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">15</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">16</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">17</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">18</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">19</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">20</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">21</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">22</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">23</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">24</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">25</span><span style=\"font-weight: bold\">]]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "a+X是\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m2\u001b[0m,  \u001b[1;36m3\u001b[0m,  \u001b[1;36m4\u001b[0m,  \u001b[1;36m5\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m \u001b[1;36m6\u001b[0m,  \u001b[1;36m7\u001b[0m,  \u001b[1;36m8\u001b[0m,  \u001b[1;36m9\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m\u001b[1;36m10\u001b[0m, \u001b[1;36m11\u001b[0m, \u001b[1;36m12\u001b[0m, \u001b[1;36m13\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m,\n",
       "\n",
       "        \u001b[1m[\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m14\u001b[0m, \u001b[1;36m15\u001b[0m, \u001b[1;36m16\u001b[0m, \u001b[1;36m17\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m\u001b[1;36m18\u001b[0m, \u001b[1;36m19\u001b[0m, \u001b[1;36m20\u001b[0m, \u001b[1;36m21\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m\u001b[1;36m22\u001b[0m, \u001b[1;36m23\u001b[0m, \u001b[1;36m24\u001b[0m, \u001b[1;36m25\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">a和X的Hadamard积是\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">8</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">12</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">14</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">16</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">18</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">20</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">22</span><span style=\"font-weight: bold\">]]</span>,\n",
       "\n",
       "        <span style=\"font-weight: bold\">[[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">24</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">26</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">28</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">30</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">32</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">34</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">36</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">38</span><span style=\"font-weight: bold\">]</span>,\n",
       "         <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">40</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">42</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">44</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">46</span><span style=\"font-weight: bold\">]]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "a和X的Hadamard积是\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m,  \u001b[1;36m2\u001b[0m,  \u001b[1;36m4\u001b[0m,  \u001b[1;36m6\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m, \u001b[1;36m10\u001b[0m, \u001b[1;36m12\u001b[0m, \u001b[1;36m14\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m\u001b[1;36m16\u001b[0m, \u001b[1;36m18\u001b[0m, \u001b[1;36m20\u001b[0m, \u001b[1;36m22\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m,\n",
       "\n",
       "        \u001b[1m[\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m24\u001b[0m, \u001b[1;36m26\u001b[0m, \u001b[1;36m28\u001b[0m, \u001b[1;36m30\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m\u001b[1;36m32\u001b[0m, \u001b[1;36m34\u001b[0m, \u001b[1;36m36\u001b[0m, \u001b[1;36m38\u001b[0m\u001b[1m]\u001b[0m,\n",
       "         \u001b[1m[\u001b[0m\u001b[1;36m40\u001b[0m, \u001b[1;36m42\u001b[0m, \u001b[1;36m44\u001b[0m, \u001b[1;36m46\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = 2\n",
    "X = torch.arange(24).reshape(2, 3, 4)\n",
    "print(f'X是\\n{X}')\n",
    "print(f'a+X是\\n{a + X}')\n",
    "print(f'a和X的Hadamard积是\\n{(a * X)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### 降维"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- 利用`sum()`计算张量元素的和，默认沿所有的轴降低张量的维度，使它变为一个标量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.584063Z",
     "iopub.status.busy": "2022-07-31T02:49:25.583278Z",
     "iopub.status.idle": "2022-07-31T02:49:25.590872Z",
     "shell.execute_reply": "2022-07-31T02:49:25.590071Z"
    },
    "origin_pos": 54,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m., \u001b[1;36m1\u001b[0m., \u001b[1;36m2\u001b[0m., \u001b[1;36m3\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">张量x的元素的和为\n",
       "<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6.0</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "张量x的元素的和为\n",
       "\u001b[1;36m6.0\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = torch.arange(4, dtype=torch.float32)\n",
    "x\n",
    "print(f'张量x的元素的和为\\n{x.sum()}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.594497Z",
     "iopub.status.busy": "2022-07-31T02:49:25.593825Z",
     "iopub.status.idle": "2022-07-31T02:49:25.600592Z",
     "shell.execute_reply": "2022-07-31T02:49:25.599705Z"
    },
    "origin_pos": 58,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m.,  \u001b[1;36m1\u001b[0m.,  \u001b[1;36m2\u001b[0m.,  \u001b[1;36m3\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m4\u001b[0m.,  \u001b[1;36m5\u001b[0m.,  \u001b[1;36m6\u001b[0m.,  \u001b[1;36m7\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m.,  \u001b[1;36m9\u001b[0m., \u001b[1;36m10\u001b[0m., \u001b[1;36m11\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m12\u001b[0m., \u001b[1;36m13\u001b[0m., \u001b[1;36m14\u001b[0m., \u001b[1;36m15\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m16\u001b[0m., \u001b[1;36m17\u001b[0m., \u001b[1;36m18\u001b[0m., \u001b[1;36m19\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">张量A的元素的和为\n",
       "<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">190.0</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "张量A的元素的和为\n",
       "\u001b[1;36m190.0\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A\n",
    "print(f'张量A的元素的和为\\n{A.sum()}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- 指定张量沿哪一个轴通过求和降低维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">张量A\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">7</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">8</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">11</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">12</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">13</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">14</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">15</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">16</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">17</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">18</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">19</span>.<span style=\"font-weight: bold\">]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "张量A\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m.,  \u001b[1;36m1\u001b[0m.,  \u001b[1;36m2\u001b[0m.,  \u001b[1;36m3\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m4\u001b[0m.,  \u001b[1;36m5\u001b[0m.,  \u001b[1;36m6\u001b[0m.,  \u001b[1;36m7\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m.,  \u001b[1;36m9\u001b[0m., \u001b[1;36m10\u001b[0m., \u001b[1;36m11\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m12\u001b[0m., \u001b[1;36m13\u001b[0m., \u001b[1;36m14\u001b[0m., \u001b[1;36m15\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m16\u001b[0m., \u001b[1;36m17\u001b[0m., \u001b[1;36m18\u001b[0m., \u001b[1;36m19\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'张量A\\n{A}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.604592Z",
     "iopub.status.busy": "2022-07-31T02:49:25.603910Z",
     "iopub.status.idle": "2022-07-31T02:49:25.611111Z",
     "shell.execute_reply": "2022-07-31T02:49:25.610252Z"
    },
    "origin_pos": 62,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">张量A沿0轴求和\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">40</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">45</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">50</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">55</span>.<span style=\"font-weight: bold\">])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "张量A沿0轴求和\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m40\u001b[0m., \u001b[1;36m45\u001b[0m., \u001b[1;36m50\u001b[0m., \u001b[1;36m55\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">求和后的形状 <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">torch.Size</span><span style=\"font-weight: bold\">([</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span><span style=\"font-weight: bold\">])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "求和后的形状 \u001b[1;35mtorch.Size\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A_sum_axis0 = A.sum(axis=0)   # 通过参数axis指定沿哪个轴求和\n",
    "print(f'张量A沿0轴求和\\n{A_sum_axis0}')\n",
    "print(f'求和后的形状 {A_sum_axis0.shape}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.614669Z",
     "iopub.status.busy": "2022-07-31T02:49:25.614007Z",
     "iopub.status.idle": "2022-07-31T02:49:25.620151Z",
     "shell.execute_reply": "2022-07-31T02:49:25.619453Z"
    },
    "origin_pos": 66,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">张量A沿1轴求和\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">22</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">38</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">54</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">70</span>.<span style=\"font-weight: bold\">])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "张量A沿1轴求和\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m6\u001b[0m., \u001b[1;36m22\u001b[0m., \u001b[1;36m38\u001b[0m., \u001b[1;36m54\u001b[0m., \u001b[1;36m70\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">求和后的形状 <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">torch.Size</span><span style=\"font-weight: bold\">([</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span><span style=\"font-weight: bold\">])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "求和后的形状 \u001b[1;35mtorch.Size\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A_sum_axis1 = A.sum(axis=1)\n",
    "print(f'张量A沿1轴求和\\n{A_sum_axis1}')\n",
    "print(f'求和后的形状 {A_sum_axis1.shape}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "```python\n",
    "A.sum(axis=[0, 1])\n",
    "```\n",
    "结果是什么？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.623974Z",
     "iopub.status.busy": "2022-07-31T02:49:25.623410Z",
     "iopub.status.idle": "2022-07-31T02:49:25.629292Z",
     "shell.execute_reply": "2022-07-31T02:49:25.628640Z"
    },
    "origin_pos": 70,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1;36m190\u001b[0m.\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.sum(axis=[0, 1])  # 先沿0轴求和，再沿1轴求和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- 利用`mean()`求平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.632603Z",
     "iopub.status.busy": "2022-07-31T02:49:25.632241Z",
     "iopub.status.idle": "2022-07-31T02:49:25.638012Z",
     "shell.execute_reply": "2022-07-31T02:49:25.637385Z"
    },
    "origin_pos": 74,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">张量A的均值为 <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9.5</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9.5</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "张量A的均值为 \u001b[1;36m9.5\u001b[0m, \u001b[1;36m9.5\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'张量A的均值为 {A.mean()}, {A.sum() / A.numel()}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- 沿轴求平均"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.640796Z",
     "iopub.status.busy": "2022-07-31T02:49:25.640448Z",
     "iopub.status.idle": "2022-07-31T02:49:25.646369Z",
     "shell.execute_reply": "2022-07-31T02:49:25.645717Z"
    },
    "origin_pos": 78,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">张量A沿0轴求平均\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">8</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">11</span>.<span style=\"font-weight: bold\">])</span>\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">8</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">11</span>.<span style=\"font-weight: bold\">])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "张量A沿0轴求平均\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m.,  \u001b[1;36m9\u001b[0m., \u001b[1;36m10\u001b[0m., \u001b[1;36m11\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m.,  \u001b[1;36m9\u001b[0m., \u001b[1;36m10\u001b[0m., \u001b[1;36m11\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'张量A沿0轴求平均\\n{A.mean(axis=0)}\\n{A.sum(axis=0) / A.shape[0]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### 非降维求和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- 计算总和或均值时保持轴数不变\n",
    "    - 在`sum()`和`mean()`中增加参数`keepdims=True`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.649666Z",
     "iopub.status.busy": "2022-07-31T02:49:25.649466Z",
     "iopub.status.idle": "2022-07-31T02:49:25.655157Z",
     "shell.execute_reply": "2022-07-31T02:49:25.654470Z"
    },
    "origin_pos": 82,
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">保持轴数沿1轴求和\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">22</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">38</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">54</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">70</span>.<span style=\"font-weight: bold\">]])</span>\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">求和后sum_A的形状为torch.Size</span><span style=\"font-weight: bold\">([</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span><span style=\"font-weight: bold\">])</span>\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">张量A的形状为torch.Size</span><span style=\"font-weight: bold\">([</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span><span style=\"font-weight: bold\">])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "保持轴数沿1轴求和\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m6\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m22\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m38\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m54\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m70\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n",
       "\u001b[1;35m求和后sum_A的形状为torch.Size\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m5\u001b[0m, \u001b[1;36m1\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n",
       "\u001b[1;35m张量A的形状为torch.Size\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m5\u001b[0m, \u001b[1;36m4\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sum_A = A.sum(axis=1, keepdims=True)\n",
    "print(f'保持轴数沿1轴求和\\n{sum_A}\\n求和后sum_A的形状为{sum_A.shape}\\n张量A的形状为{A.shape}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "\\begin{problem}\\label{prob:lowerDim}\n",
    "A除以sum_A结果是什么？\n",
    "\\end{problem}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.658240Z",
     "iopub.status.busy": "2022-07-31T02:49:25.657743Z",
     "iopub.status.idle": "2022-07-31T02:49:25.663240Z",
     "shell.execute_reply": "2022-07-31T02:49:25.662591Z"
    },
    "origin_pos": 86,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m.,  \u001b[1;36m1\u001b[0m.,  \u001b[1;36m2\u001b[0m.,  \u001b[1;36m3\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m4\u001b[0m.,  \u001b[1;36m5\u001b[0m.,  \u001b[1;36m6\u001b[0m.,  \u001b[1;36m7\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m.,  \u001b[1;36m9\u001b[0m., \u001b[1;36m10\u001b[0m., \u001b[1;36m11\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m12\u001b[0m., \u001b[1;36m13\u001b[0m., \u001b[1;36m14\u001b[0m., \u001b[1;36m15\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m16\u001b[0m., \u001b[1;36m17\u001b[0m., \u001b[1;36m18\u001b[0m., \u001b[1;36m19\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m6\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m22\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m38\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m54\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m70\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m0.0000\u001b[0m, \u001b[1;36m0.1667\u001b[0m, \u001b[1;36m0.3333\u001b[0m, \u001b[1;36m0.5000\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m0.1818\u001b[0m, \u001b[1;36m0.2273\u001b[0m, \u001b[1;36m0.2727\u001b[0m, \u001b[1;36m0.3182\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m0.2105\u001b[0m, \u001b[1;36m0.2368\u001b[0m, \u001b[1;36m0.2632\u001b[0m, \u001b[1;36m0.2895\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m0.2222\u001b[0m, \u001b[1;36m0.2407\u001b[0m, \u001b[1;36m0.2593\u001b[0m, \u001b[1;36m0.2778\u001b[0m\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m0.2286\u001b[0m, \u001b[1;36m0.2429\u001b[0m, \u001b[1;36m0.2571\u001b[0m, \u001b[1;36m0.2714\u001b[0m\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A\n",
    "sum_A\n",
    "A / sum_A  # 广播机制"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- 沿某个轴计算张量的元素的累积总和\n",
    "    - 不会降低张量的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.666763Z",
     "iopub.status.busy": "2022-07-31T02:49:25.666257Z",
     "iopub.status.idle": "2022-07-31T02:49:25.672211Z",
     "shell.execute_reply": "2022-07-31T02:49:25.671493Z"
    },
    "origin_pos": 90,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">张量A沿0轴的累积总和为\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">8</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">12</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">15</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">18</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">21</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">24</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">28</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">32</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">36</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">40</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">45</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">50</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">55</span>.<span style=\"font-weight: bold\">]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "张量A沿0轴的累积总和为\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m.,  \u001b[1;36m1\u001b[0m.,  \u001b[1;36m2\u001b[0m.,  \u001b[1;36m3\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m4\u001b[0m.,  \u001b[1;36m6\u001b[0m.,  \u001b[1;36m8\u001b[0m., \u001b[1;36m10\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m12\u001b[0m., \u001b[1;36m15\u001b[0m., \u001b[1;36m18\u001b[0m., \u001b[1;36m21\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m24\u001b[0m., \u001b[1;36m28\u001b[0m., \u001b[1;36m32\u001b[0m., \u001b[1;36m36\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m40\u001b[0m., \u001b[1;36m45\u001b[0m., \u001b[1;36m50\u001b[0m., \u001b[1;36m55\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'张量A沿0轴的累积总和为\\n{A.cumsum(axis=0)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### 点积"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- 点积是按相同位置的元素乘积的和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "```python\n",
    "torch.dot(input, other)\n",
    "```\n",
    "- `input`和`other`都必须是**1维的张量**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.675225Z",
     "iopub.status.busy": "2022-07-31T02:49:25.674701Z",
     "iopub.status.idle": "2022-07-31T02:49:25.681409Z",
     "shell.execute_reply": "2022-07-31T02:49:25.680678Z"
    },
    "origin_pos": 94,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">x为tensor</span><span style=\"font-weight: bold\">([</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>.<span style=\"font-weight: bold\">])</span>, <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">y为tensor</span><span style=\"font-weight: bold\">([</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>.<span style=\"font-weight: bold\">])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "\u001b[1;35mx为tensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m., \u001b[1;36m1\u001b[0m., \u001b[1;36m2\u001b[0m., \u001b[1;36m3\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m, \u001b[1;35my为tensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m1\u001b[0m., \u001b[1;36m1\u001b[0m., \u001b[1;36m1\u001b[0m., \u001b[1;36m1\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">x和y的点积为\n",
       "<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6.0</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "x和y的点积为\n",
       "\u001b[1;36m6.0\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "y = torch.ones(4, dtype = torch.float32)\n",
    "print(f'x为{x}, y为{y}')\n",
    "print(f'x和y的点积为\\n{torch.dot(x, y)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- 点积等同于按元素乘法再求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1;36m6\u001b[0m.\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(x*y).sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### 矩阵与向量乘积"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- 矩阵$\\mathbf{A}$（$m\\times n$维）与向量$\\mathbf{x}$（$n$维）的乘积是一个长度为$m$的列向量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "```python\n",
    "torch.mv(input,vec)\n",
    "```\n",
    "- `input`：$m\\times n$维的张量（矩阵）\n",
    "- `vec`：$n$维的向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.693867Z",
     "iopub.status.busy": "2022-07-31T02:49:25.693189Z",
     "iopub.status.idle": "2022-07-31T02:49:25.698677Z",
     "shell.execute_reply": "2022-07-31T02:49:25.697979Z"
    },
    "origin_pos": 105,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">矩阵A\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">7</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">8</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">11</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">12</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">13</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">14</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">15</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">16</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">17</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">18</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">19</span>.<span style=\"font-weight: bold\">]])</span>\n",
       "向量x\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>.<span style=\"font-weight: bold\">])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "矩阵A\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m.,  \u001b[1;36m1\u001b[0m.,  \u001b[1;36m2\u001b[0m.,  \u001b[1;36m3\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m4\u001b[0m.,  \u001b[1;36m5\u001b[0m.,  \u001b[1;36m6\u001b[0m.,  \u001b[1;36m7\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m.,  \u001b[1;36m9\u001b[0m., \u001b[1;36m10\u001b[0m., \u001b[1;36m11\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m12\u001b[0m., \u001b[1;36m13\u001b[0m., \u001b[1;36m14\u001b[0m., \u001b[1;36m15\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m16\u001b[0m., \u001b[1;36m17\u001b[0m., \u001b[1;36m18\u001b[0m., \u001b[1;36m19\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n",
       "向量x\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m., \u001b[1;36m1\u001b[0m., \u001b[1;36m2\u001b[0m., \u001b[1;36m3\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'矩阵A\\n{A}\\n向量x\\n{x}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Ax的乘积为\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">14</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">38</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">62</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">86</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">110</span>.<span style=\"font-weight: bold\">])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "Ax的乘积为\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m14\u001b[0m.,  \u001b[1;36m38\u001b[0m.,  \u001b[1;36m62\u001b[0m.,  \u001b[1;36m86\u001b[0m., \u001b[1;36m110\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'Ax的乘积为\\n{torch.mv(A, x)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### 矩阵与矩阵相乘"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- 矩阵$\\mathbf{A}$（$n\\times p$维）与矩阵$\\mathbf{B}$（$p\\times m$维）的乘积形成一个$n \\times m$维矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "```python\n",
    "torch.mm(input,mat2)\n",
    "```\n",
    "- `input`：第一个$n\\times p$维的张量（矩阵）\n",
    "- `mat2`：第二个$p\\times m$维的张量（矩阵）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.702116Z",
     "iopub.status.busy": "2022-07-31T02:49:25.701512Z",
     "iopub.status.idle": "2022-07-31T02:49:25.707439Z",
     "shell.execute_reply": "2022-07-31T02:49:25.706770Z"
    },
    "origin_pos": 109,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">矩阵A为\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">7</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">8</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">10</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">11</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">12</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">13</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">14</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">15</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">16</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">17</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">18</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">19</span>.<span style=\"font-weight: bold\">]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "矩阵A为\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m0\u001b[0m.,  \u001b[1;36m1\u001b[0m.,  \u001b[1;36m2\u001b[0m.,  \u001b[1;36m3\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m4\u001b[0m.,  \u001b[1;36m5\u001b[0m.,  \u001b[1;36m6\u001b[0m.,  \u001b[1;36m7\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m \u001b[1;36m8\u001b[0m.,  \u001b[1;36m9\u001b[0m., \u001b[1;36m10\u001b[0m., \u001b[1;36m11\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m12\u001b[0m., \u001b[1;36m13\u001b[0m., \u001b[1;36m14\u001b[0m., \u001b[1;36m15\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m16\u001b[0m., \u001b[1;36m17\u001b[0m., \u001b[1;36m18\u001b[0m., \u001b[1;36m19\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">矩阵B为\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>.<span style=\"font-weight: bold\">]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "矩阵B为\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m\u001b[1;36m1\u001b[0m., \u001b[1;36m1\u001b[0m., \u001b[1;36m1\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m1\u001b[0m., \u001b[1;36m1\u001b[0m., \u001b[1;36m1\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m1\u001b[0m., \u001b[1;36m1\u001b[0m., \u001b[1;36m1\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m1\u001b[0m., \u001b[1;36m1\u001b[0m., \u001b[1;36m1\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'矩阵A为\\n{A}')\n",
    "B = torch.ones(4, 3)\n",
    "print(f'矩阵B为\\n{B}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">矩阵A和B的乘积为\n",
       "<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">tensor</span><span style=\"font-weight: bold\">([[</span> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>.,  <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">22</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">22</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">22</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">38</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">38</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">38</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">54</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">54</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">54</span>.<span style=\"font-weight: bold\">]</span>,\n",
       "        <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">70</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">70</span>., <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">70</span>.<span style=\"font-weight: bold\">]])</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "矩阵A和B的乘积为\n",
       "\u001b[1;35mtensor\u001b[0m\u001b[1m(\u001b[0m\u001b[1m[\u001b[0m\u001b[1m[\u001b[0m \u001b[1;36m6\u001b[0m.,  \u001b[1;36m6\u001b[0m.,  \u001b[1;36m6\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m22\u001b[0m., \u001b[1;36m22\u001b[0m., \u001b[1;36m22\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m38\u001b[0m., \u001b[1;36m38\u001b[0m., \u001b[1;36m38\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m54\u001b[0m., \u001b[1;36m54\u001b[0m., \u001b[1;36m54\u001b[0m.\u001b[1m]\u001b[0m,\n",
       "        \u001b[1m[\u001b[0m\u001b[1;36m70\u001b[0m., \u001b[1;36m70\u001b[0m., \u001b[1;36m70\u001b[0m.\u001b[1m]\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'矩阵A和B的乘积为\\n{torch.mm(A, B)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## 范数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "\\begin{definition}\\label{def:norm}\n",
    "范数（norm）：衡量一个向量的大小。$L^p$范数定义如下：\n",
    "\\end{definition}\n",
    "\n",
    "$$\\|\\mathbf{x}\\|_p = \\left(\\sum_{i=1}^n \\left|x_i\\right|^p\\right)^{\\frac{1}{p}}$$\n",
    "\n",
    "$p\\in\\mathbb{R}, p\\ge 1$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "\\begin{definition}\\label{def:L2}\n",
    "$L_2$*范数*：向量元素**平方和的平方根**：\n",
    "\\end{definition}\n",
    "\n",
    "$$\\|\\mathbf{x}\\|_2 = \\sqrt{\\sum_{i=1}^n x_i^2}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.710886Z",
     "iopub.status.busy": "2022-07-31T02:49:25.710388Z",
     "iopub.status.idle": "2022-07-31T02:49:25.715538Z",
     "shell.execute_reply": "2022-07-31T02:49:25.714892Z"
    },
    "origin_pos": 113,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">向量u的L2范数为 <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5.0</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "向量u的L2范数为 \u001b[1;36m5.0\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "u = torch.tensor([3.0, -4.0])\n",
    "print(f'向量u的L2范数为 {torch.norm(u)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "\\begin{definition}\\label{def:L1}\n",
    "$L_1$范数：向量元素的**绝对值之和**\n",
    "\\end{definition}\n",
    "\n",
    "$$\\|\\mathbf{x}\\|_1 = \\sum_{i=1}^n \\left|x_i \\right|$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.718921Z",
     "iopub.status.busy": "2022-07-31T02:49:25.718450Z",
     "iopub.status.idle": "2022-07-31T02:49:25.723592Z",
     "shell.execute_reply": "2022-07-31T02:49:25.722954Z"
    },
    "origin_pos": 117,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">向量u的L1范数为 <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">7.0</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "向量u的L1范数为 \u001b[1;36m7.0\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f'向量u的L1范数为 {torch.abs(u).sum()}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "\\begin{definition}\\label{def:FN}\n",
    "矩阵的*Frobenius范数*（Frobenius norm）：矩阵元素**平方和的平方根**\n",
    "\\end{definition}\n",
    "\n",
    "$$\\|\\mathbf{X}\\|_F = \\sqrt{\\sum_{i=1}^m \\sum_{j=1}^n x_{ij}^2}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T02:49:25.726935Z",
     "iopub.status.busy": "2022-07-31T02:49:25.726429Z",
     "iopub.status.idle": "2022-07-31T02:49:25.731749Z",
     "shell.execute_reply": "2022-07-31T02:49:25.731145Z"
    },
    "origin_pos": 121,
    "slideshow": {
     "slide_type": "fragment"
    },
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">矩阵V的Frobenius范数为 <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">6.0</span>\n",
       "</pre>\n"
      ],
      "text/plain": [
       "矩阵V的Frobenius范数为 \u001b[1;36m6.0\u001b[0m\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "V = torch.ones((4, 9))\n",
    "print(f'矩阵V的Frobenius范数为 {torch.norm(V)}')"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "幻灯片",
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": true,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "rise": {
   "autolaunch": false,
   "enable_chalkboard": true,
   "scroll": true
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "307.2px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  },
  "vscode": {
   "interpreter": {
    "hash": "34418ffb6f02e522390adb0e13441cc75f901cd11cccb4f6f613643b4b4d2a0b"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
